package cn.xianshu.web.controller;

import cn.xianshu.CourseConstans.CourseDto;
import cn.xianshu.service.ICourseService;
import cn.xianshu.domain.Course;
import cn.xianshu.query.CourseQuery;
import cn.xianshu.util.AjaxResult;
import cn.xianshu.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController {



    @Autowired
    public ICourseService courseService;

    /**
    * 下线发布
    */
    @PreAuthorize("hasAuthority('course:offLineCourse')")
    @RequestMapping(value="/offLineCourse/{id}",method= RequestMethod.POST)
    public AjaxResult offLineCourse(@PathVariable("id") Long id){
        courseService.offLineCourse(id);
        return AjaxResult.me().setSuccess(true).setMessage("下线成功!");
    }

    /**
    * 上线发布
    */
    @PreAuthorize("hasAuthority('course:onLineCourse')")
    @RequestMapping(value="/onLineCourse/{id}",method= RequestMethod.POST)
    public AjaxResult onLineCourse(@PathVariable("id") Long id){
        courseService.onLineCourse(id);
        return AjaxResult.me().setSuccess(true).setMessage("上线成功!");
    }

    /**
    * 保存课程添加
    */
    @PreAuthorize("hasAuthority('course:save')")
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody CourseDto Dto){
        courseService.save(Dto);
        return AjaxResult.me().setSuccess(true).setMessage("保存成功!");
    }

    /**
    * 删除对象
    */
    @PreAuthorize("hasAuthority('course:delete')")
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return AjaxResult.me();
    }

   /**
   * 根据ID获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj(courseService.selectById(id));
    }


    /**
    * 获取所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        return AjaxResult.me().setResultObj(courseService.selectList(null));
    }

    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public AjaxResult json(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        PageList pageList = new PageList<Course>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }
}
